Week 3 - tmap

1 Rapa Nui / Isla de Pascua ((668715.4 7002628, 668776.6 7002640, 668796

Data

The example data is taken from the Elegant and informative maps with tmap book - see the data folder in the github repository.

Filetypes:

  • tif files are geospatial raster data (e.g. elevation maps)

  • gpkg are geopackage files (modern version of shapefiles)

Easter Island data - elevation

(elev = read_stars("data/easter_island/ei_elev.tif"))
stars object with 2 dimensions and 1 attribute
attribute(s):
             Min.  1st Qu.   Median     Mean  3rd Qu.     Max.   NA's
ei_elev.tif     0 56.98041 114.3601 143.5146 204.9752 506.8161 619721
dimension(s):
  from   to  offset delta                refsys point x/y
x    1 1060  651409    25 WGS 84 / UTM zone 12S FALSE [x]
y    1  832 7008921   -25 WGS 84 / UTM zone 12S FALSE [y]

plot(elev)

Easter Island data - border

(border = read_sf("data/easter_island/ei_border.gpkg"))
Simple feature collection with 1 feature and 1 field
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: 653566.4 ymin: 6990751 xmax: 675697.4 ymax: 7006462
Projected CRS: WGS 84 / UTM zone 12S
# A tibble: 1 × 2
  name                                                            geom
  <chr>                                                  <POLYGON [m]>
1 Rapa Nui / Isla de Pascua ((668715.4 7002628, 668776.6 7002640, 668…

plot(border)

Easter Island data - roads

(roads = read_sf("data/easter_island/ei_roads.gpkg"))
Simple feature collection with 71 features and 3 fields
Geometry type: MULTILINESTRING
Dimension:     XY
Bounding box:  xmin: 654205.9 ymin: 6991936 xmax: 671167.7 ymax: 7004570
Projected CRS: WGS 84 / UTM zone 12S
# A tibble: 71 × 4
   name          type        strokelwd                            geom
   <chr>         <chr>           <dbl>           <MULTILINESTRING [m]>
 1 Ahu Vinapu    secondary           7 ((657423.6 6993598, 657497.1 6…
 2 Ana o Rohi    residential         1 ((656665.1 6996059, 656656.9 6…
 3 Ara Ao Ātea   residential         1 ((659126.8 6996222, 659202.7 6…
 4 Ara Haha Nau  residential         1 ((659201.4 6996096, 659198 699…
 5 Ara Kio'e     residential         1 ((657206 6995242, 657233.3 699…
 6 Ara Roa Rakei secondary           7 ((656134.8 6996142, 656156.3 6…
 7 Ara Te ‘Uira  residential         1 ((659262.4 6996299, 659184.8 6…
 8 Atamu Te Kena primary            10 ((655836.6 6996189, 655802.7 6…
 9 Atamu Te Kena tertiary            4 ((656056 6996383, 656040 69963…
10 Avareipua     residential         1 ((655652.1 6995697, 655664.6 6…
# … with 61 more rows

plot(roads %>% select(type))

Easter Island data - points of interest

(points = read_sf("data/easter_island/ei_points.gpkg"))
Simple feature collection with 65 features and 4 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 654386.2 ymin: 6991802 xmax: 674296.4 ymax: 7006172
Projected CRS: WGS 84 / UTM zone 12S
# A tibble: 65 × 5
   name              type    elevation    sv               geom
   <chr>             <chr>       <dbl> <int>        <POINT [m]>
 1 Eo                volcano        NA     2   (669750 6998584)
 2 Tere Vaka         volcano       506     1 (660580.1 7003005)
 3 Pu'a Katiki       volcano       370     2 (672981.6 7000699)
 4 Rano Kau          volcano       320     1 (655785.4 6991802)
 5 Perehe            volcano       216     5 (673470.4 7001878)
 6 Tea Tea           volcano       280     2 (673371.4 7001681)
 7 Te Hoŋa (O Koro)  volcano       326     5 (663647.8 7001194)
 8 O Tu'u            volcano       284     9 (660587.5 6997813)
 9 Te Kauhaŋa o Varu volcano       240     7 (660639.3 6998830)
10 Hau Epa           volcano        70     8 (666464.4 7004348)
# … with 55 more rows

plot(points %>% select(type), pch=16)

Layers with ggplot

ggplot() +
  geom_sf(data=border) +
  scale_fill_distiller(palette="RdYlGn", na.value = "transparent") +
  geom_stars(data=elev) +
  geom_sf(data=roads, aes(linewidth=strokelwd)) +
  scale_linewidth_continuous(range = c(0.1,1)) +
  geom_sf(data=points, aes(color=type, size=elevation), shape=17) +
  scale_size_continuous(range=c(1,4)) +
  theme_minimal() +
  guides(linewidth="none")

tmap

tmap

tmap is an actively maintained open-source R-library for drawing thematic maps. The API is based on A Layered Grammar of Graphics and resembles the syntax of ggplot2, a popular R-library for drawing charts.

library(tmap)

Easter Island - Border

tm_shape(border) +
  tm_borders()

Easter Island - Border

tm_shape(border) +
  tm_borders() +
  tm_layout(
    main.title = "Easter Island",
    bg.color="lightblue"
  )

Roads

tm_shape(border) +
  tm_borders() +
tm_shape(roads) +
  tm_lines(lwd="strokelwd", legend.lwd.show = FALSE, ) +
tm_layout(
  main.title = "Easter Island",
  bg.color="lightblue"
)

Roads

tm_shape(border) +
  tm_borders() +
tm_shape(roads) +
  tm_lines(
    lwd="strokelwd", 
    legend.lwd.show = FALSE, 
    col = "type"
  ) +
tm_layout(
  main.title = "Easter Island",
  bg.color="lightblue"
)

Elevation

tm_shape(elev) +
  tm_raster() +
tm_shape(border) +
  tm_borders() +
tm_shape(roads) +
  tm_lines(
    lwd="strokelwd", 
    legend.lwd.show = FALSE
  ) +
tm_layout(
  main.title = "Easter Island",
  bg.color="lightblue"
)

Elevation

tm_shape(elev) +
  tm_raster(
    style = "cont",
    title = "Elevation (m)"
  ) +
tm_shape(border) +
  tm_borders() +
tm_shape(roads) +
  tm_lines(
    lwd="strokelwd", 
    legend.lwd.show = FALSE
  ) +
tm_layout(
  main.title = "Easter Island",
  bg.color="lightblue"
)

tm_shape(elev) +
  tm_raster(
    style = "cont",
    title = "Elevation (m)",
    palette = "-RdYlGn"
  ) +
tm_shape(border) +
  tm_borders() +
tm_shape(roads) +
  tm_lines(
    lwd="strokelwd", 
    legend.lwd.show = FALSE
  ) +
tm_layout(
  main.title = "Easter Island",
  bg.color="lightblue"
)

Volcanoes

tm_shape(elev) +
  tm_raster(
    style = "cont",
    title = "Elevation (m)",
    palette = "-RdYlGn"
  ) +
tm_shape(border) +
  tm_borders() +
tm_shape(roads) +
  tm_lines(
    lwd="strokelwd", 
    legend.lwd.show = FALSE
  ) +
tm_shape(
  points %>% filter(type == "volcano")
) + 
  tm_symbols(
    shape = 24,
    size = "elevation",
    title.size = "Volcanoes (m)"
  ) +
tm_layout(
  main.title = "Easter Island",
  bg.color="lightblue"
)

Volcanoes

Other stuff

tm_graticules(alpha=0.25) +
tm_shape(elev) +
  tm_raster(
    style = "cont",
    title = "Elevation (m)",
    palette = "-RdYlGn"
  ) +
tm_shape(border) +
  tm_borders() +
tm_shape(roads) +
  tm_lines(
    lwd="strokelwd", 
    legend.lwd.show = FALSE
  ) +
tm_shape(
  points %>% filter(type == "volcano")
) + 
  tm_symbols(
    shape = 24,
    size = "elevation",
    title.size = "Volcanoes (m)"
  ) +
tm_layout(
  main.title = "Easter Island",
  bg.color="lightblue"
) +
tm_compass(position = c("right", "top")) +
tm_scale_bar() +
tm_credits("Author, 2021") +
tm_add_legend(type = "line", col = "black", title = "Roads")

Other stuff

tmap modes

map = tm_graticules(alpha=0.25) +
tm_shape(elev) +
  tm_raster(
    style = "cont",
    title = "Elevation (m)",
    palette = "-RdYlGn"
  ) +
tm_shape(border) +
  tm_borders() +
tm_shape(roads) +
  tm_lines(
    lwd="strokelwd", 
    legend.lwd.show = FALSE
  ) +
tm_shape(
  points %>% filter(type == "volcano")
) + 
  tm_symbols(
    shape = 24,
    size = "elevation",
    title.size = "Volcanoes (m)"
  ) +
tm_layout(
  main.title = "Easter Island",
  bg.color="lightblue"
) +
tm_compass(position = c("right", "top")) +
tm_scale_bar() +
tm_credits("Author, 2021") +
tm_add_legend(type = "line", col = "black", title = "Roads")

plot

tmap_mode("plot")
map

view

tmap_mode("view")
map

Tiles

tm_basemap(
  c( osm  = "OpenStreetMap",
     esri = "Esri.WorldStreetMap")
) +
tm_shape(border, is.master = TRUE) +
  tm_borders() +
tm_shape(roads) +
  tm_lines(
    lwd="strokelwd", 
    legend.lwd.show = FALSE
  ) +
tm_shape(
  points %>% filter(type == "volcano")
) + 
  tm_symbols(
    size = "elevation",
    legend.size.show = FALSE
  )

Tiles

Tiles w/ plot mode

tmap_mode("plot")
tm_basemap(
  c( osm  = "OpenStreetMap",
     esri = "Esri.WorldStreetMap")
) +
tm_shape(border, is.master = TRUE) +
  tm_borders() +
tm_shape(roads) +
  tm_lines(
    lwd="strokelwd", 
    legend.lwd.show = FALSE
  ) +
tm_shape(
  points %>% filter(type == "volcano")
) + 
  tm_symbols(
    size = "elevation",
    legend.size.show = FALSE
  )

Tiles w/ plot mode

tmap_mode("plot")
tm_shape(
  tmaptools::read_osm(border)
) +
  tm_rgb() +
tm_shape(border, is.master = TRUE) +
  tm_borders() +
tm_shape(roads) +
  tm_lines(
    lwd="strokelwd", 
    legend.lwd.show = FALSE
  ) +
tm_shape(
  points %>% filter(type == "volcano")
) + 
  tm_symbols(
    size = "elevation",
    legend.size.show = FALSE
  )